#include"common.h"

int main()
{
    int sockfd=socket(AF_INET,SOCK_STREAM,0);
    struct sockaddr_in serv_addr;
    bzero(&serv_addr,sizeof(serv_addr));
    serv_addr.sin_family=AF_INET;
    serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
    serv_addr.sin_port=htons(8080);
    connect(sockfd,(sockaddr*)&serv_addr,sizeof(serv_addr));
    while(true)
    {
        //buff做写缓冲区
        char buff[BUFFER_NUM];
        bzero(buff,sizeof(buff));
        scanf("%s",buff);
        ssize_t write_bytes=write(sockfd,buff,sizeof(buff));//写到fd里面
        if(write_bytes==-1)
        {
            printf("socket already disconnected, we can't write any more\n");
            break;
        }
        //buff做读缓冲区
        bzero(buff,sizeof(buff));
        ssize_t read_bytes=read(sockfd,buff,sizeof(buff));
        if(read_bytes>0)
        {
            buff[read_bytes]=0;
            printf("message from server:%s\n",buff);
        }
        else if(read_bytes==0)
        {
            printf("server already disconnect\n");
            break;
        }
        else
        {
            close(sockfd);
            printf("socket read error\n");
        }
        
    }
    return 0;
}